package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.ExamineSellerNum;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.seller.SellerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 运营商后台 商家管理模块（商家审核 商家管理）
 * @Author Eayon
 * @Date 2020/2/27 10:44
 */
@Controller
@RequestMapping("seller")
public class SellerController {

    private Logger log = LoggerFactory.getLogger(SellerController.class);

    @Reference
    private SellerService sellerService;

    /**
     * 跳转商家审核页 并查询所有待审核的商家信息(状态为0)
     * @return
     */
    @RequestMapping("toExamineSeller")
    public String toExamineSeller(Model model){
        List<TbSeller> sellers = sellerService.getNotExamineSellerInfo();
        model.addAttribute("sellers",sellers);
        return "seller/seller_1";
    }

    /**
     * 根据公司名称或者店铺名称模糊查询未审核的商家信息
     * @param name
     * @param nickName
     * @return
     */
    @RequestMapping(value = "getLikeNotExamineSeller")
    @ResponseBody
    public DataResult getLikeNotExamineSeller(String name,String nickName){
        List<TbSeller> sellers = null;
        try {
            sellers = sellerService.getLikeNotExamineSeller(name,nickName);
        }catch (Exception e){
            log.error("根据公司名称或者店铺名称模糊查询未审核的商家信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),sellers);
    }

    /**
     * 根据主键id查询商家信息
     * @param id
     * @return
     */
    @RequestMapping(value = "findSellerById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult findSellerById(String id){
        TbSeller seller = null;
        try {
            seller = sellerService.findSellerById(id);
        }catch (Exception e){
            log.error("根据主键id查询商家信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),seller);
    }

    /**
     * 商铺审核  flag参数三种状态 1=审核通过 2=审核不通过 3=关闭商铺
     * @param
     * @param flag
     * @return
     */
    @RequestMapping(value = "updateSellerStatusById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateSellerStatusById(TbSeller seller,String flag){
        try {
            sellerService.updateSellerStatusById(seller,flag);
        }catch (Exception e){
            log.error("审核商品异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
}
